var table;
var table1;
$(function(){
    table=$("#monitor").DataTable({
        "ajax": {
            url:"../../admin/monitor/Get",
            type:"POST",
            data:function(d){
                d.id_num=$("#monitor_idNum").val()
                d.name=$("#monitor_name").val()
                d.ca_num=$("#monitor_caNum").val()
                d.org_name=$("#monitor_orgName").val()
                d.date=$("#monitor_date").val()
                d.monitor=$("#monitor_monitor").val()
            },error:function(XMLHttpRequest,textStatus,errorThrown){
                console.log("载入数据出错");
            }
        },
        "scrollX": 1000,
        "bProcessing": true,
        "bServerSide": true,
        "bPaginate": true,
        "bAutoWidth": true,
        "bInfo": true,//页脚信息
        "bFilter": false,//禁用搜索
        "bSort":false,//禁用排序
        "aLengthMenu": [2,5,10],
        "sPaginationType":'full_numbers',
        "columns": [
            {"data":"id"},
            {"data":"id"},
            { "data": "name" },
            { "data": "id_num" },
            { "data": "ca_num" },
            { "data": "org_name" },
            { "data": "date" },
            { "data":"monitor"},
            { "data":"id"}
        ],
        "aoColumnDefs": [
            {
                "aTargets": [ 8 ],
                "mRender": function ( data, type, full ) {
                    return '<a onclick="editData_monitor(\''+full.id+'\',\''+full.id_num+'\',\''+full.name+'\',' +
                        '\''+full.ca_num+'\',\''+full.org_name+'\',\''+full.date+'\',\''+full.monitor+'\')">修改</a>' +
                        '<a style="margin-left: 12px" onclick="delData_monitor(\''+full.id+'\')">删除</a>';
                }
            },
            {
                "aTargets": [ 1 ],
                "mRender": function ( data, type, full ) {
                    return '<input type="checkbox" name="tab_box_monitor" value="'+full.id+'" onclick="checkId_monitor(\''+full.id+'\')">';
                }
            }
        ],
        "fnRowCallback": function (nRow, aData, iDisplayIndex) {
            $("td:first", nRow).html(iDisplayIndex + 1);
            return nRow;
        },
        "oLanguage": {
            "sProcessing": "处理中...",
            "sLengthMenu": "每页 _MENU_ 条",
            "sZeroRecords": "没有匹配结果",
            "sInfo": " _START_ 至 _END_ 项结果，共 _TOTAL_ 项",//显示第 _START_ 至 _END_ 项结果，共 _TOTAL_ 项
            "sInfoEmpty": "",//显示第 0 至 0 项结果，共 0 项
            "sInfoFiltered": "",//(由 _MAX_ 项结果过滤)
            "sInfoPostFix": "",
            "sSearch": "搜索：",
            "sUrl": "",
            "sEmptyTable": "没有数据呀!",
            "sLoadingRecords": "载入中...",
            "sInfoThousands": ",",
            "oPaginate": {
                "sFirst": "首页",
                "sPrevious": "上页",
                "sNext": "下页",
                "sLast": "末页"
            }
        },
    });
    importConfig($);
})



new SMValidator('#editMonitor');

function searchBtn_monitor(){
    table.draw();
}

function modal_monitor_sub(){
    if(SMValidator.validate('#editMonitor')){
        $.ajax({
            url: "../../admin/monitor",
            type: "POST",
            dataType: "json",
            data: $("#editMonitor").serialize(),
            beforeSend: function () {
            },
            success: function (data) {
                if(data){
                    var msg = ""
                    if($("#id").val()){
                        msg = "修改成功"
                    }else {
                        msg = "新增成功"
                    }
                    Lobibox.notify('success', {
                        msg: msg,
                        sound: false
                    });
                    $("#editMonitor").modal("hide")
                    table.draw();
                }else {
                    console.log(data)
                }
            }, error: function () {
            }
        });
    }
}

function editData_monitor(id, id_num, name, ca_num, org_name, date, monitor) {
    $("#id").val(id)
    $("#add_idNum").val(id_num)
    $("#add_name").val(name)
    $("#add_caNum").val(ca_num)
    $("#add_orgName").val(org_name)
    $("#add_date").val(date)
    $("#add_Monitor").val(monitor)
    $("#myMonitor").modal("show")
}

function delData_monitor(id){
    Lobibox.confirm({
        msg: "确认要删除?",
        title:"删除",
        iconClass: false,
        callback: function ($this, type, ev) {
            if(type==='yes'){
                $.ajax({
                    url: "../../admin/monitor/"+id+"?_csrf="+$("input[name=_csrf]").val(),
                    type: "DELETE",
                    dataType: "json",
                    success: function (data) {
                        if(data){
                            table.draw();
                            Lobibox.notify('success', {
                                msg: '删除成功',
                                sound: false
                            });
                        }else {
                            Lobibox.notify('error', {
                                msg: '删除失败',
                                sound: false
                            });
                        }
                    }, error: function () {
                    }
                });
            }
        }
    });
}

function import_monitor() {
    $("#monitor-modal-form").modal("show");
}

function importConfig($) {
    $("#importMonitorFile").ace_file_input({
        no_file:'选择文件 ...',
        btn_choose:'选择',
        btn_change:'选择',
        droppable:false,
        thumbnail:false, //| true | large
        whitelist:'xlsx|xls'
    }).on('change', function(){
        var names=$(this).data('ace_input_files')[0].name.split(".")
        if(names[names.length-1]!=="xlsx"&&names[names.length-1]!=="xls"){
            alert("只接受xlsx或xls文件")
            return
        }
    });
}

function submitMonitorFile() {
    $("#monitorFileForm").ajaxSubmit({
        type: 'POST',
        dataType: "json",
        url:"../../admin/monitor/import",
        success: function(data){
            if(data){
                Lobibox.notify('success', {
                    msg: "导入成功",
                    sound: false
                });
                window.location.reload();
            }else {
                Lobibox.notify('error', {
                    msg: "导入失败",
                    sound: false
                });
            }
            $("#monitor-modal-form").modal("hide")
        },
        error: function(XmlHttpRequest, textStatus, errorThrown){
            alert( "error");
        }
    })
}

function output_monitor(){
    window.location.href="../../admin/monitor/exportEmail.html";
}
